/**
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/17 22:47
 */
package org.mechanic.fund.web.product;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.mechanic.core.web.MediaTypes;
import org.mechanic.fund.domain.product.ProductAttachmentType;
import org.mechanic.fund.service.product.IProductAttachmentTypeService;
import org.mechanic.fund.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 产品附件类型 API
 *
 * @author mechanic
 * @version 0.0.1
 */
@Api(value = "产品附件类型接口")
@RestController
@RequestMapping(value = "product/attachement/type")
public class ProductAttachmentTypeController extends BaseController {

    private final IProductAttachmentTypeService attachmentTypeService;

    @Autowired
    public ProductAttachmentTypeController(IProductAttachmentTypeService attachmentTypeService) {
        this.attachmentTypeService = attachmentTypeService;
    }

    @ApiOperation(value = "返回所有产品附件类型列表")
    @RequestMapping(value = "list", method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public Object list(
      @ApiParam(value = "页码(从0开始)", required = false, defaultValue = "0")
      @RequestParam(required = true, defaultValue = "0") Integer page) {
        //设置排序
        Sort sort = new Sort(new Order(Direction.ASC, "sort"));
        //查询列表
        Page<ProductAttachmentType> pages = this.attachmentTypeService.getProductAttachmentTypeList(super.pageRequest(
          page,
          sort
        ));
        return result(pages);
    }

    @ApiOperation(value = "批量保存产品附件类型")
    @RequestMapping(value = "save", method = RequestMethod.PUT, consumes = MediaTypes.JSON_UTF_8)
    public Object save(
      @ApiParam(value = "产品附件类型集合", required = true) @RequestBody(required = true)
        List<ProductAttachmentType> productAttachmentTypes) {
        this.attachmentTypeService.save(productAttachmentTypes, super.currUser().getId());
        return result();
    }

    @ApiOperation(value = "按 ID 删除产品附件类型")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.DELETE, produces = MediaTypes.JSON_UTF_8)
    public Object delete(
      @ApiParam(value = "产品附件类型 ID", required = true) @PathVariable(required = true) String id) {
        this.attachmentTypeService.delete(id);
        return result();
    }
}
